package com.example.spark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.spark.entity.Leader;
import com.example.spark.entity.dto.PaginationDTO;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.LeaderService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/leader")
public class LeaderController {

    private final LeaderService leaderService;

    public LeaderController(LeaderService leaderService) {
        this.leaderService = leaderService;
    }

    /**
     * 获取负责人，首页展示
     */
    @GetMapping("/getLeader")
    public CommonResult<List<Leader>> getLeader(){
        return leaderService.getLeaders();
    }
    /**
     * 获取负责人
     */
    @PostMapping  ("/getAllLeader")
    public CommonResult<IPage<Leader>> getAll(@RequestBody PaginationDTO<Leader> pageDto){
        return leaderService.getAll(pageDto);
    }

    /**
     * 添加负责人
     * @param leader 添加的负责人信息
     */
    @PostMapping("/saveLeader")
    public CommonResult<Leader> saveOne(@RequestBody Leader leader) throws IOException {
        return leaderService.saveOne(leader);
    }

    /**
     * 删除负责人
     * @param id 删除的负责人id
     */
    @DeleteMapping("/deleteLeader")
    public CommonResult<Long> deleteById(@RequestParam Long id){
        return leaderService.deleteById(id);
    }

    /**
     * 修改负责人信息
     */
    @PutMapping("/updateLeader")
    public CommonResult<Leader> updateOne(@RequestBody Leader leader){
        return leaderService.updateOne(leader);
    }

    /**
     * 修改负责人图片
     */
    @PostMapping("/updateLeader/photo")
    public CommonResult<String> updatePhoto(@RequestParam MultipartFile photo, @RequestParam Long id) throws IOException {
        return leaderService.updatePhoto(photo,id);
    }
}
